package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 14-6-20.
 */
public class Biblioteca {
    private List<Book> bookList;
    private List<String> menuList = new ArrayList<String>();
    private List<Book> checkOutList = new ArrayList<Book>();
    private Map<String, String> userMap = new HashMap<String, String>();

    {
        menuList.add("List Books");
        menuList.add("Checkout Book");
        menuList.add("Return Book");
        menuList.add("Quit");
        userMap.put("111-1111", "111");
    }
    private List<Movie> movieList;

    public Biblioteca(List<Book> bookList) {
        this.bookList = bookList;
    }

    public Biblioteca() {
    }

    public Biblioteca(List<Book>bookList, List<Movie> movieList) {
        this.bookList = bookList;
        this.movieList = movieList;
    }

    public List<Movie> getMovieList() {
        return movieList;
    }

    public String getInto() {
        return "Welcome";
    }

    public List<Book> getBookList() {
        return bookList;
    }
    public String getBooksDetails() {
        String details = "";
        for (Book book : bookList) {
            details += book.getDetails();
        }
        return details;
    }

    public String getMoviesDetails() {
        String details = "";
        for (Movie movie : movieList) {
            details += movie.getDetails();
        }
        return details;
    }

    public String getMenuDetails() {
        String details = "";
        for (int i = 1; i <= menuList.size(); i++) {
            String menu = menuList.get(i - 1);
            details += i + "--" + menu + "\n";
        }
        return details;
    }

    public String chooseOption(int optionNum) {
        if (isValidOption(optionNum)) {
            return menuList.get(optionNum - 1);
        }
        return "Select a valid option";
    }

    public boolean isValidOption(int optionNum) {
        return optionNum > 0 && optionNum <= menuList.size();
    }

    public String checkOutForBook(String bookName) {
        for (Book book : bookList) {
            if (book.getName().equals(bookName)) {
                bookList.remove(book);
                checkOutList.add(book);
                return "Thank you! Enjoy the books.";
            }
        }
        return "That book is not available.";
    }

    public String checkOutForMovie(String name) {
        for (Movie movie : movieList) {
            if (movie.getName().equals(name)) {
                return "Thank you! Enjoy the movie.";
            }
        }
        return "That movie is not available.";
    }

    public String returnedBook(String bookName) {
        for (Book book : checkOutList) {
            if (book.getName().equals(bookName)) {
                bookList.add(book);
                checkOutList.remove(book);
                return "Thank you for returning the book.";
            }
        }
        return "That is not a valid book to return.";
    }

    public boolean check(String libNum, String password) {
        for (String libNo : userMap.keySet()) {
            if(libNum.equals(libNo)){
                return userMap.get(libNo).equals(password);
            }
        }
        return false;

    }
}
